import { setToken, delToken } from '@/utils/auth'
import request from '@/utils/request'

// const res = await axios.post('http://geek.itheima.net/v1_0/authorizations', {


export const loginAction = (formDate) => {
  const { mobile, code } = formDate
  return async dispatch => {
    // 1.获取token
    const { data } = await request.post('/authorizations', {
      mobile,
      code
    })
    // console.log(data.token)
    // 注意：此处获取的是 token 不是 refresh_token
    // 2.存储token到redux
    const { token } = data
    dispatch({ type: 'login/token', token: token })
    // 3.本地存储
    // localStorage.setItem('geek-pc-token', token)
    setToken(token)
  }
}

export const logout = () => {
  return (dispatch) => {
    // 1 清除 token
    dispatch({ type: 'login/clearToken' })
    // 清除本地 token
    delToken()
    // 2 清除个人信息
    dispatch({ type: 'user/clearInfo' })
  }
}